package assignment1.eventplan.utils;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.NotificationCompat;
import android.widget.RemoteViews;

import assignment1.eventplan.R;


public final class NotificationHelper {

    private static volatile int id = 1;
    public static final boolean LOLLIPOP = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;

    public static int defineId() {
        return id++;
    }

    public static void notify(Context context, CharSequence ticker, CharSequence title, CharSequence content, PendingIntent intent, Long when, final int notificationId) {
        notify(context, ticker, title, content, intent, when, notificationId, true, NotificationCompat.PRIORITY_DEFAULT);
    }

    public static void notify(Context context, CharSequence ticker, CharSequence title, CharSequence content, PendingIntent intent, Long when, final int notificationId, int priority) {
        notify(context, ticker, title, content, intent, when, notificationId, true, priority);
    }

    public static void notify(Context context, CharSequence ticker, CharSequence title, CharSequence content,
                              PendingIntent intent, Long when, final int notificationId, boolean autoCancel, int priority) {
        if (null == content || null == intent)
            return;
        notify(context, builder(context, ticker, title, content, intent, when, notificationId, autoCancel, priority), notificationId);
    }

    public static NotificationCompat.Builder builder(Context context, CharSequence ticker, CharSequence title, CharSequence content,
                                                     PendingIntent intent, Long when, final int notificationId, boolean autoCancel, int priority) {
        final NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
        builder.setTicker(ticker);
        builder.setContentTitle(title);
        builder.setContentText(content);
        builder.setContentIntent(intent);
        builder.setDefaults(Notification.DEFAULT_SOUND);
        builder.setPriority(priority);
        builder.setAutoCancel(autoCancel);
        builder.setStyle(new android.support.v4.app.NotificationCompat.BigTextStyle().bigText(content));
        if (null != when)
            builder.setWhen(when);
        return builder;
    }

    public static void notify(Context context, final NotificationCompat.Builder builder, final int notificationId) {
        notify(context, builder, notificationId, build(context));
    }

    public static void notify(Context context, final NotificationCompat.Builder builder, final int notificationId, final NotificationManagerCompat manager) {
        notify(context, builder, notificationId, manager,
                R.mipmap.ic_launcher,
                R.mipmap.ic_launcher
        );
    }


    public static void notify(Context context, final NotificationCompat.Builder builder, final int notificationId,
                              final NotificationManagerCompat manager, int resId, int transparentResId) {
        if (LOLLIPOP) {
            builder.setSmallIcon(transparentResId);
        } else {
            builder.setColor(Color.WHITE);
            builder.setSmallIcon(resId);
        }
        manager.notify(notificationId, builder.build());
    }

    public static void notify(Context context, RemoteViews contentViews, PendingIntent intent, final int notificationId, boolean autoCancel) {
        notify(context, NotificationManagerCompat.from(context), contentViews, intent, notificationId, autoCancel, NotificationCompat.PRIORITY_DEFAULT);
    }

    public static void notify(Context context, NotificationManagerCompat manager, RemoteViews contentViews, PendingIntent intent, final int notificationId, boolean autoCancel) {
        notify(context, manager, contentViews, intent, notificationId, autoCancel, NotificationCompat.PRIORITY_DEFAULT);
    }

    public static void notify(Context context, NotificationManagerCompat manager, RemoteViews contentViews, PendingIntent intent, final int notificationId, boolean autoCancel, int priority) {
        if (null == contentViews || null == intent)
            return;
        final NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
        builder.setContentIntent(intent);
        builder.setPriority(priority);
        builder.setAutoCancel(autoCancel);
        builder.setContent(contentViews);
        notify(context, builder, notificationId, manager);
    }

    public static Intent maybeNeedNewTaskFlag(Intent intent, boolean isFromPush) {
        if (isFromPush)
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        return intent;
    }

    public static PendingIntent buildPendingIntentWithParentStack(Context context, Intent intent) {
        return buildPendingIntentWithParentStack(context, intent, 0/* requestCode */);
    }

    public static PendingIntent buildPendingIntentWithParentStack(Context context, Intent intent, int requestCode) {
        if (null == context || null == intent)
            return null;
        return TaskStackBuilder.create(context)
                .addNextIntentWithParentStack(intent)
                .getPendingIntent(requestCode /* requestCode */, PendingIntent.FLAG_UPDATE_CURRENT);
    }


    public static Notification build(Context context, RemoteViews contentViews, PendingIntent intent) {
        final NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
        builder.setContentIntent(intent);
        builder.setAutoCancel(false);
        builder.setContent(contentViews);
        return builder.build();
    }

    public static NotificationManagerCompat build(Context context) {
        return NotificationManagerCompat.from(context);
    }

}
